const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token:'',
    newsPasswordVal:'',
    confirmNewsPasswordVal:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      token: options.token
    })
  },
  // 获取新密码
  newsPasswordInp:function(e){
    this.setData({
      newsPasswordVal: e.detail.value
    })
  },
  // 获取确认新密码
  confirmNewsPasswordInp: function (e) {
    this.setData({
      confirmNewsPasswordVal: e.detail.value
    })
  },
  // 确认
  confirmBtn:function(){
    let { newsPasswordVal, confirmNewsPasswordVal, token } = this.data;
    if (newsPasswordVal == ''){
      wx.showToast({
        title: '请输入新密码',
        icon: 'none',
        duration: 2000
      });
      return false
    }
    if (confirmNewsPasswordVal == '') {
      wx.showToast({
        title: '请输入确认新密码',
        icon: 'none',
        duration: 2000
      });
      return false
    }
    if (newsPasswordVal === confirmNewsPasswordVal){
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: app.globalData.baseUrl + 'user/passwordReset',
        data: {
          password: newsPasswordVal,
          repassword: confirmNewsPasswordVal,
          token: token
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          console.log(res)
          wx.hideLoading();
          if (res.data.code == 1000) {
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            })
            setTimeout(function(){
              wx.navigateTo({
                url: '/userPage/pages/login/login',
              })
            },2000)
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }else{
      wx.showToast({
        title: '密码输入不一致，请重新输入',
        icon: 'none',
        duration: 2000
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
